package com.home.base.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.home.biz.ActivityBiz;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.stream.Collectors;

/**
 * @author ：chenxf
 * @date ：Created in 2020/3/19 15:18
 * @description：
 * @modified By：
 * @version: $
 */
@Slf4j
public class FetchNumUtil2 implements Callable<String> {
    private ActivityBiz activityBiz;
    /**
     * C002
     * 2003130012433867 -- 一生一号
     * 1910170012348890 --  靓号
     * 1910170012348890 -- 4G飞享18元档
     * 1910230012354413 -- 4G飞享（分销）18元档
     */
    String param="PAGE_NUM=1&PAGE_SIZE=8&ACTIVITY_ID=#ACTIVITY&IS_OTHER_NET=F&pageType=null&HLR=null&NUM_POSITION=#NUM&channel_id=#CHANNEL";
    String param2="PAGE_NUM=1&PAGE_SIZE=8&ACTIVITY_ID=#ACTIVITY&IS_OTHER_NET=F&pageType=null&HLR=null&NUM_POSITION=#NUM";
   //PAGE_NUM=1&PAGE_SIZE=10&ACTIVITY_ID=2001200012395003&PACKAGE_FLAG=YLXK19YDC&LEVEL_ID=2001200012395014&NUM_DUAN=135&IS_OTHER_NET=F&pageType=null&HLR=null&NUM_POSITION=
    String paramYLXK="PAGE_NUM=1&PAGE_SIZE=8&ACTIVITY_ID=#ACTIVITY&PACKAGE_FLAG=YLXK19YDC&LEVEL_ID=2001200012395014&&IS_OTHER_NET=F&pageType=null&HLR=null&NUM_POSITION=#NUM";
   String  provName="", cityName="",data="",activiName="";
    String url="http://wap.cq.10086.cn/openh5/server/packageDetailController/geunum";
     String refer="http://wap.cq.10086.cn/openh5/static/numberMall/numberOrderNew";
     String refer2="https://wap.cq.10086.cn/openh5/static/numberMall/liangHaoNumber/lianghaoIndex.html";
    public FetchNumUtil2(String data){
        this.data=data;

    }
    public FetchNumUtil2(ActivityBiz activityBiz, String provName, String cityName, String activiName, String acitviCode, String num){
        this.data=param.replace("#ACTIVITY",acitviCode).replace("#NUM",num).replace("#CHANNEL","C002");
        this.activityBiz=activityBiz;
        this.provName=provName;
        this.cityName=cityName;
       // this.acitviCode=acitviCode;
        this.activiName=activiName;
    }

    public FetchNumUtil2(ActivityBiz activityBiz, String provName, String cityName, String activiName, String acitviCode, String num, boolean type){
        this.data=paramYLXK.replace("#ACTIVITY",acitviCode).replace("#NUM",num).replace("#CHANNEL","C002");
        this.activityBiz=activityBiz;
        this.provName=provName;
        this.cityName=cityName;
        // this.acitviCode=acitviCode;
        this.activiName=activiName;
    }

    public FetchNumUtil2(ActivityBiz activityBiz, String provName, String cityName, String activiName, String acitviCode, String num, int type){
        this.data=param2.replace("#ACTIVITY",acitviCode).replace("#NUM",num).replace("#CHANNEL","C001");
        this.activityBiz=activityBiz;
        this.provName=provName;
        this.cityName=cityName;
        // this.acitviCode=acitviCode;
        this.activiName=activiName;
    }

    public void save() {

        try {
            activityBiz.saveList(provName,cityName,activiName,fetch());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List<String> fetch() throws Exception {
        log.debug("fetch data"+data);

        log.debug("fetch data"+new Des().strEnc(data));
        String doc = Jsoup.connect(url)
                .data("param",new Des().strEnc(data))
                .data("channel_id","C001")
                .data("isVisitor","0")
                .ignoreContentType(true)
                .header("Referer",refer2)
                .method(Connection.Method.GET)
                .header("Content-Type","application/json")
                .userAgent("Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)")
               // .cookies(cookie)
                .execute()
                .body();
        JSONObject obj= JSON.parseObject(doc);

        JSONObject result=obj.getJSONObject("num");
        if(result!=null){
            log.debug("fetch finished");
            List<Map> list=JSON.parseArray(result.getJSONArray("RESULT").toJSONString(),Map.class);

            return  list.stream().map(m->m.get("SERIAL_NUMBER").toString()).collect(Collectors.toList());

        }else {
            log.error("采集异常");
        }
        return null;
    }

    @Override
    public String call() throws Exception {
        save();
        return data+"fetch finish";
    }

    public static void main(String[] args){
        String s="PAGE_NUM=1&PAGE_SIZE=8&ACTIVITY_ID=1910170012348890&IS_OTHER_NET=F&pageType=null&HLR=null&NUM_POSITION=1022&channel_id=C001";
        String s2="PAGE_NUM=1&PAGE_SIZE=8&ACTIVITY_ID=1910170012348890&IS_OTHER_NET=F&pageType=null&HLR=null&NUM_POSITION=1022";
        FetchNumUtil2 fetchNumUtil=new FetchNumUtil2(s );
        try {
        List<String> result=fetchNumUtil.fetch();
           System.out.println(result.size());
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
